/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package net.alteiar.plateform.game.view;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;

import net.alteiar.plateform.game.Scale;
import net.alteiar.plateform.game.World;
import net.alteiar.plateform.game.WorldObserver;
import net.alteiar.plateform.game.model.Entity;

/**
 * @author Cody Stoutenburg
 * 
 */
public class UiWorld implements WorldObserver {
	private final World world;

	private final EntityViewBuilder viewBuilder;

	private final List<EntityView> allEntity;

	private final Scale scale;
	private final long frameInMs;

	private final Integer visibleWidthPixel;
	private final Integer visibleHeightPixel;

	private Integer xOffsetPixel;
	private Integer yOffsetPixel;

	public UiWorld(World world, long frameInMs) {
		this.world = world;
		this.frameInMs = frameInMs;
		this.allEntity = new ArrayList<EntityView>();

		this.scale = new Scale(40);

		this.visibleWidthPixel = 800;
		this.visibleHeightPixel = 600;

		this.xOffsetPixel = 0;
		this.yOffsetPixel = 0;

		this.viewBuilder = new EntityViewBuilder(this);

		// generate all existing entity:
		for (Entity character : world.getAllCharacters()) {
			this.allEntity.add(viewBuilder.generateEntityView(character));
		}
		this.world.addObserver(this);
	}

	@Override
	public void entitieAdded(Entity entity) {
		allEntity.add(viewBuilder.generateEntityView(entity));
	}

	public Scale getScale() {
		return this.scale;
	}

	public long getFrameInMs() {
		return frameInMs;
	}

	public Integer getVisiblePixelWidth() {
		return visibleWidthPixel;
	}

	public Integer getVisiblePixelHeight() {
		return visibleHeightPixel;
	}

	private Long getPixelWidth() {
		return scale.getPixels() * this.world.getWidth();
	}

	private Long getPixelHeight() {
		return scale.getPixels() * this.world.getHeight();
	}

	/**
	 * move in world
	 * 
	 * @param x
	 * @param y
	 */
	public void move(Integer x, Integer y) {
		xOffsetPixel += x;
		yOffsetPixel += y;

		if ((xOffsetPixel + visibleWidthPixel) > this.getPixelWidth()) {
			xOffsetPixel = 0;
		}

		if ((yOffsetPixel + visibleHeightPixel) > this.getPixelHeight()) {
			yOffsetPixel = 0;
		}

		if (xOffsetPixel < 0) {
			xOffsetPixel = 0;
		}
		if (yOffsetPixel < 0) {
			yOffsetPixel = 0;
		}

	}

	public int getXOffsetPixel() {
		return xOffsetPixel;
	}

	public int getYOffsetPixel() {
		return yOffsetPixel;
	}

	public int getXMaxPixel() {
		return xOffsetPixel + visibleWidthPixel;
	}

	public int getYMaxPixel() {
		return yOffsetPixel + visibleHeightPixel;
	}

	public void draw(Graphics2D g) {
		// background
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, visibleWidthPixel, visibleHeightPixel);

		final int beginWorldPixelX = xOffsetPixel;
		final int beginWorldPixelY = yOffsetPixel;
		final int maxWorlPixelX = xOffsetPixel + visibleWidthPixel;
		final int maxWorldPixelY = yOffsetPixel + visibleHeightPixel;

		int xEnd = visibleWidthPixel;
		int yEnd = visibleHeightPixel;

		if (maxWorlPixelX > this.getPixelWidth()) {
			xEnd = (int) (this.getPixelWidth() - xOffsetPixel);
		}
		if (maxWorldPixelY > this.getPixelHeight()) {
			yEnd = (int) (this.getPixelHeight() - yOffsetPixel);
		}

		g.setColor(new Color(138, 54, 15));
		g.fillRect(0, 0, xEnd, yEnd);

		// define border
		g.setColor(Color.BLACK);
		/*
		 * g.setStroke(new BasicStroke(2)); for (long i = xOffsetPixel; i <
		 * xOffsetPixel + visibleWidthPixel; ++i) { int visibleX =
		 * Long.valueOf(i - xPositiveOffset).intValue(); if (i %
		 * this.scale.getPixels() == 0) { g.drawLine(visibleX, yBegin, visibleX,
		 * yEnd); } } for (long i = yPositiveOffset; i < yPositiveOffset +
		 * visibleHeightPixel; ++i) { int visibleY = Long.valueOf(i -
		 * yPositiveOffset).intValue(); if (i % this.scale.getPixels() == 0) {
		 * g.drawLine(xBegin, visibleY, xEnd, visibleY); } }
		 */

		for (EntityView element : allEntity) {
			element.draw(g);
		}
	}
}
